package com.magist.manage.web.controller;

import com.github.pagehelper.PageInfo;
import com.magist.core.controller.BaseController;
import com.magist.core.entity.ResultMessage;
import com.magist.core.exception.BusinessException;
import com.magist.core.util.BeanCopyUtil;
import com.magist.manage.web.dto.MailAcceptUsersDto;
import com.magist.manage.web.dto.UserInfoDto;
import com.magist.manage.web.entity.MailAcceptUsers;
import com.magist.manage.web.entity.UserInfo;
import com.magist.manage.web.entity.UserLogin;
import com.magist.manage.web.service.MailAcceptUsersService;
import com.magist.manage.web.service.UserInfoService;
import com.magist.manage.web.service.UserLoginService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.assertj.core.util.Lists;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import springfox.documentation.annotations.ApiIgnore;
import tk.mybatis.mapper.entity.Example;

import java.util.List;

@Api(value="邮件用户接口",tags={"邮件用户接口"})
@RestController
@RequestMapping("/rest/mailAcceptUsers")
public class RestMailAcceptUsersController extends BaseController {

	@Autowired
	private MailAcceptUsersService mailAcceptUsersService;
	@Autowired
	private UserInfoService userInfoService;
	@Autowired
	private UserLoginService userLoginService;

	@ApiOperation(value="保存用户邮箱", notes="保存用户邮箱接口")
	@RequestMapping(value = "doSaveMailUsers.do", method = RequestMethod.POST)
	public Object selectUsers(@ModelAttribute("userIds")String[] userIds) {
		mailAcceptUsersService.insertMailUsers(userIds);
		return ResultMessage.success();
	}

	@ApiIgnore
	@ApiOperation(value="更新", notes="更新接口")
	@RequestMapping(value = "update.do", method = RequestMethod.POST)
	public Object update(@ModelAttribute("mailAcceptUsersDto") MailAcceptUsersDto mailAcceptUsersDto) {
		MailAcceptUsers oldMailAcceptUsers = mailAcceptUsersService.getById(mailAcceptUsersDto.getId());
		if(oldMailAcceptUsers==null)
			throw new BusinessException("无效的数据!");
		ResultMessage result =null;
		MailAcceptUsers mailAcceptUsers = new MailAcceptUsers();
		BeanUtils.copyProperties(mailAcceptUsersDto,mailAcceptUsers);
		mailAcceptUsers.setModifyTime(oldMailAcceptUsers.getModifyTime());
		if (mailAcceptUsersService.update(mailAcceptUsers)>0) 
			result = ResultMessage.success("操作成功");
		else
			result=ResultMessage.error("操作失败!");
		return result;
	}

	@ApiOperation(value="列表", notes="分页查询接口")
	@RequestMapping(value = "page.do", method = RequestMethod.GET)
	@ApiImplicitParams({
			@ApiImplicitParam(name = "userName", value = "用户姓名", required = false, dataType = "String", paramType = "query"),
			@ApiImplicitParam(name = "mailAddress", value = "邮箱", required = false, dataType = "String", paramType = "query"),
	})
	public Object page(String userName,String mailAddress) {
		ResultMessage result = ResultMessage.success();
		MailAcceptUsers mailAcceptUsers = new MailAcceptUsers();
		if(!StringUtils.isEmpty(userName)) {
			mailAcceptUsers.setUserName("%" +userName +"%");
		}
		if(!StringUtils.isEmpty(mailAddress)) {
			mailAcceptUsers.setMailAddress("%" +mailAddress +"%");
		}
		PageInfo<MailAcceptUsers> pageInfo = mailAcceptUsersService.page(mailAcceptUsers,getPageInfo());
		result.setContent(pageInfo);
		return result;
	}

	@ApiOperation(value="删除", notes="删除接口")
	@RequestMapping(value = "del.do", method = RequestMethod.POST)
	public Object del(@ModelAttribute("id") String id) {
		MailAcceptUsers mailAcceptUsers = mailAcceptUsersService.getById(id);
		if(mailAcceptUsers==null)
			throw new BusinessException("无效的数据!");
		ResultMessage result =null;
		if(mailAcceptUsersService.delete(mailAcceptUsers)>0)
			result = ResultMessage.success("操作成功");
		else
			result=ResultMessage.error("操作失败!");
		return result;
	}

	@ApiIgnore
	@ApiOperation(value="详情", notes="详情接口")
	@RequestMapping(value = "info.do", method = RequestMethod.GET)
	public Object info(@ModelAttribute("id") String id) {
		MailAcceptUsers mailAcceptUsers = mailAcceptUsersService.getById(id);
		if(mailAcceptUsers==null)
			throw new BusinessException("无效的数据!");
		MailAcceptUsersDto mailAcceptUsersDto = new MailAcceptUsersDto();
		BeanUtils.copyProperties(mailAcceptUsers,mailAcceptUsersDto);
		ResultMessage result = ResultMessage.success();
		result.setContent(mailAcceptUsersDto);
		return result;
	}

	@ApiOperation(value="查询可供勾选用户列表", notes="查询可供勾选用户列表")
	@RequestMapping(value = "pageUserInfo.do", method = RequestMethod.GET)
	@ApiImplicitParams({
		@ApiImplicitParam(name = "userName", value = "用户姓名", required = false, dataType = "String", paramType = "query"),
		@ApiImplicitParam(name = "userStatus", value = "状态", required = false, dataType = "String", paramType = "query"),
		@ApiImplicitParam(name = "userType", value = "类型", required = false, dataType = "String", paramType = "query"),
	})
	public Object pageUserInfo(String userName,String userStatus,String userType) {
		ResultMessage result = ResultMessage.success();
		// 查询已勾选的
		List<MailAcceptUsers> mailAcceptUsers = mailAcceptUsersService.find(new MailAcceptUsers());
		Example example = new Example(UserInfo.class);
		example = packExample(example, mailAcceptUsers);
		PageInfo<UserInfo> pageInfo = userInfoService.page(example,getPageInfo());
		List<UserInfo> list = pageInfo.getList();
		List<UserInfoDto> dtoList = Lists.newArrayList();
		for (UserInfo userInfo : list) {
			UserInfoDto copyDto = BeanCopyUtil.copy(userInfo, UserInfoDto.class);
			UserLogin userLogin = userLoginService.getById(userInfo.getId());
			if(!ObjectUtils.isEmpty(userLogin)) {
				copyDto.setEmail(userLogin.getEmail());
				dtoList.add(copyDto);
			}
		}
		PageInfo<UserInfoDto> pageDto = BeanCopyUtil.copy(pageInfo, PageInfo.class);
		pageDto.setList(dtoList);
		result.setContent(pageDto);
		return result;
	}

	private Example packExample(Example example,List<MailAcceptUsers> mailAcceptUsers) {
		Example.Criteria criteria = example.createCriteria();
		criteria.andEqualTo("deleted", "F");
		if (!CollectionUtils.isEmpty(mailAcceptUsers)) {
			mailAcceptUsers.forEach(mailUser ->{
				criteria.andNotEqualTo("id", mailUser.getUserId());
			});
		}
		return example;
	}

}
